﻿using System;
using System.Configuration;
using System.Globalization;
using System.Windows.Forms;
using GimmeRainbow.Abstract;
using GimmeRainbow.Properties;

namespace GimmeRainbow
{
    public partial class ConfigurationForm : Form
    {
        public ConfigurationForm()
        {
            InitializeComponent();
        }

        private void ConfigurationFormLoad(object sender, EventArgs e)
        {
            ResetForm();
        }


        protected void ResetForm()
        {
            # region GeneralConfig

            cboHashType.DataSource = Enum.GetNames(typeof (Abstract.HelperClass.HashType));
            cboHashType.SelectedIndex = cboHashType.Items.Count - 1;
            txtOutputDirectory.Text = Resources.ConfigurationForm_ResetForm_OutputFileDirectory;
            txtLogFile.Text = Resources.ConfigurationForm_ResetForm_LogFile;
            chkCreateFilePerHash.Checked = true;
            chkEnableLogging.Checked = true;

            # endregion

            # region HashingConfig

            chkGenWithoutSalt.Checked = true;
            nudRepetation.Value = 1000;

            # endregion

        }

        private void TxtLogFileMouseDown(object sender, MouseEventArgs e)
        {
            txtLogFile.Select();
            HelperClass.SetDirectoryInTextBox(txtLogFile);
        }

        private void TxtOutputDirectoryMouseDown(object sender, MouseEventArgs e)
        {
            txtOutputDirectory.Select();
            HelperClass.SetDirectoryInTextBox(txtOutputDirectory);
        }

        private void BtnOkayClick(object sender, EventArgs e)
        {
            //ConfigurationManager.AppSettings["nudRepeatations"] = nudRepetation.Value.ToString(CultureInfo.InvariantCulture);
            var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            configFile.AppSettings.Settings.Clear();
            configFile.AppSettings.Settings.Add("HashType", cboHashType.SelectedItem.ToString());
            configFile.AppSettings.Settings.Add("OutputDirectory", txtOutputDirectory.Text);
            configFile.AppSettings.Settings.Add("LogFile", txtLogFile.Text);
            configFile.AppSettings.Settings.Add("PerHashFile", chkCreateFilePerHash.Checked ? "True" : "False");
            configFile.AppSettings.Settings.Add("EnableLogging", chkEnableLogging.Checked ? "True" : "False");
            configFile.AppSettings.Settings.Add("GenerateHashWithoutSalt", chkGenWithoutSalt.Checked ? "True" : "False");
            configFile.AppSettings.Settings.Add("NumberOfRepeatations", nudRepetation.Value.ToString(CultureInfo.InvariantCulture));
            configFile.Save();
            Close();
        }

    }
}
